﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Admin_Default : PageBase
{
    int currentProductID;

    protected void Page_Load(object sender, EventArgs e)
    {
        //身份验证
        if (!IsAdmin)
        {
            Response.Redirect("./");
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (ddlProduct.SelectedValue==null)
        {
            this.ShowMPE("请先选择商品，再为该商品上传图片");
            return;
        }

        int.TryParse(ddlProduct.SelectedValue, out currentProductID);
        if (currentProductID == 0)
        {
            this.ShowMPE("请先选择商品，再为该商品上传图片");
            return;
        }

        try
        {
            StringBuilder msg = new StringBuilder();

            List<NCCITPubSaleModel.Image> list = new List<NCCITPubSaleModel.Image>();

            // Get the HttpFileCollection
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    hpf.SaveAs(Server.MapPath("ProductImages") + "\\" +
                      System.IO.Path.GetFileName(hpf.FileName));

                    msg.AppendLine("<b>File: </b>");
                    msg.Append(hpf.FileName);
                    msg.Append(" <b>Size:</b> ");
                    msg.Append(hpf.ContentLength);
                    msg.Append(" <b>Type:</b> ");
                    msg.Append(hpf.ContentType);
                    msg.Append(" Uploaded Successfully <br/>");
                }
                list.Add(new NCCITPubSaleModel.Image() { Path = "ProductImages/" + System.IO.Path.GetFileName(hpf.FileName), LogTime = DateTime.Now });
            }
            using (NCCITPubSaleModel.NCCITPubSaleEntities ctx = new NCCITPubSaleModel.NCCITPubSaleEntities())
            {
                var product = ctx.Product.Where(p => p.ID == currentProductID).FirstOrDefault();
                product.Image.Load();

                foreach (NCCITPubSaleModel.Image img in list)
                {
                    if (product.Image.Where(i => i.Path == img.Path).Count() == 0)
                    {
                        product.Image.Add(img);
                    }
                }
                ctx.SaveChanges();
            }

            this.ShowMPE(msg.ToString());
            this.BindGrid();
        }
        catch (Exception ex)
        {
            this.ShowMPE("上传失败，错误信息为：" + ex.Message);
        }
    }

    protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProduct.SelectedValue == "")
        {
            return;
        }
        BindGrid();
    }

    private void BindGrid()
    {
        using (NCCITPubSaleModel.NCCITPubSaleEntities ctx = new NCCITPubSaleModel.NCCITPubSaleEntities())
        {
            int id = int.Parse(this.ddlProduct.SelectedValue);

            var imglist = ctx.Image.Where(i => i.Product.ID == id);

            this.GridView1.DataSource = imglist;
            this.GridView1.DataBind();
        }
        this.CurrentProduct.InnerText = ddlProduct.SelectedItem.Text;
        this.currentProductID = Convert.ToInt32(ddlProduct.SelectedValue);
    }

    private void BindGrid(int currentPage)
    {
        using (NCCITPubSaleModel.NCCITPubSaleEntities ctx = new NCCITPubSaleModel.NCCITPubSaleEntities())
        {
            int id = int.Parse(this.ddlProduct.SelectedValue);

            var imglist = ctx.Image.Where(i => i.Product.ID == id);

            this.GridView1.DataSource = imglist;
            this.GridView1.PageIndex = currentPage;
            this.GridView1.DataBind();
        }
        this.CurrentProduct.InnerText = ddlProduct.SelectedItem.Text;
        this.currentProductID = Convert.ToInt32(ddlProduct.SelectedValue);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BindGrid(e.NewPageIndex);
    }

    private void ShowMPE(string msg)
    {
        this.MPE2.InnerHtml = msg;
        this.ModalPopupExtender2.Show();
    }
}
